/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.delivery.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.MyOrderDto;
import com.yami.shop.bean.app.dto.MyOrderItemDto;
import com.yami.shop.bean.app.dto.OrderSelfStationDto;
import com.yami.shop.bean.model.Order;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.delivery.common.model.OrderSelfStation;
import com.yami.shop.delivery.common.service.OrderSelfStationService;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.MyOrderService;
import com.yami.shop.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author Lanhai
 */
@RestController
@RequestMapping("/p/myStationOrder")
@Api(tags = "用户自提订单接口")
@AllArgsConstructor
public class MyStationOrderController {

    private final OrderService orderService;

    private final MyOrderService myOrderService;

    private final MapperFacade mapperFacade;

    private final OrderSelfStationService orderSelfStationService;

    @GetMapping("/stationDetail")
    @ApiOperation(value = "订单详情自提信息", notes = "根据订单号获取订单详情自提信息")
    @ApiImplicitParam(name = "orderNumber", value = "订单号", required = true, dataType = "String")
    public ServerResponseEntity<OrderSelfStationDto> orderDetail(@RequestParam(value = "orderNumber") String orderNumber) {
        OrderSelfStationDto orderSelfStationDto = orderSelfStationService.getStationByOrderNumber(orderNumber);
        return ServerResponseEntity.success(orderSelfStationDto);
    }

    @GetMapping("/getOrderBystationId")
    @ApiOperation(value = "用户扫自提点码", notes = "用户扫自提点码获取自提订单、虚拟商品订单待核销列表")
    @ApiImplicitParam(name = "stationId", value = "自提点id", required = true, dataType = "Long")
    public ServerResponseEntity<IPage<MyOrderDto>> getOrderListByStationId(PageParam<MyOrderDto> pageParam,@RequestParam(value = "stationId") Long stationId) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<MyOrderDto> page = myOrderService.getOrderListByStationId(pageParam,stationId, userId);
        return ServerResponseEntity.success(page);
    }

    @PutMapping("/orderStationByOrderNumber")
    @ApiOperation(value = "用户提货操作", notes = "用户提货操作")
    public ServerResponseEntity<Boolean> orderStationByOrderNumber(@RequestBody MyOrderDto myOrderDto) {
        List<String> orderNumberList = Arrays.asList(myOrderDto.getOrderNumbers().split(","));
        return ServerResponseEntity.success(orderService.orderStationByOrderNumber(orderNumberList, SecurityUtils.getUser().getUserId(), myOrderDto.getStationId()));
    }

    @GetMapping("/getStationInfoByOrderNumber")
    @ApiOperation(value = "我的订单自提信息接口", notes = "根据订单号获取订单详情自提信息")
    @ApiImplicitParam(name = "orderNumber", value = "订单号", required = true, dataType = "String")
    public ServerResponseEntity<MyOrderDto> getStationDetailByOrderNumber(@RequestParam(value = "orderNumber") String orderNumber) {
        OrderSelfStation orderSelfStation = orderSelfStationService.getOne(new LambdaQueryWrapper<OrderSelfStation>()
                .eq(OrderSelfStation::getOrderNumber, orderNumber));
        OrderSelfStationDto orderSelfStationDto = mapperFacade.map(orderSelfStation, OrderSelfStationDto.class);
        Order order = orderService.getOrderAndOrderItemByOrderNumber(orderNumber);
        //获取订单项的退款信息
        orderService.getOrderItemRefundStatus(order);
        MyOrderDto myOrderDto = mapperFacade.map(order, MyOrderDto.class);
        myOrderDto.setOrderItemDtos(mapperFacade.mapAsList(order.getOrderItems(), MyOrderItemDto.class));
        myOrderDto.setOrderSelfStationDto(orderSelfStationDto);
        return ServerResponseEntity.success(myOrderDto);
    }
}
